﻿using DotNetCoreStudy.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DotNetCoreStudy.Web.services
{
    public class EmployeeService : BaseService, IEmployeeService
    {
        public readonly List<Employee> _employees = new List<Employee>();
        public EmployeeService()
        {
            _employees.Add(new Employee
            {
                Id = 1,
                DepartmentId = 1,
                FirstName = "Nick",
                LastName = "Carter",
                Gender = EnumGender.Male,
                Fired = false
            });
            _employees.Add(new Employee
            {
                Id = 2,
                DepartmentId = 1,
                FirstName = "Michael",
                LastName = "Jackson",
                Gender = EnumGender.Male
            });
            _employees.Add(new Employee
            {
                Id = 3,
                DepartmentId = 1,
                FirstName = "Mariah",
                LastName = "Carey",
                Gender = EnumGender.Female
            });
            _employees.Add(new Employee
            {
                Id = 4,
                DepartmentId = 2,
                FirstName = "Axl",
                LastName = "Rose",
                Gender = EnumGender.Male
            });
            _employees.Add(new Employee
            {
                Id = 4,
                DepartmentId = 2,
                FirstName = "Kate",
                LastName = "Winslet",
                Gender = EnumGender.Female
            });
            _employees.Add(new Employee
            {
                Id = 6,
                DepartmentId = 3,
                FirstName = "Rob",
                LastName = "Thomas",
                Gender = EnumGender.Male
            });
            _employees.Add(new Employee
            {
                Id = 7,
                DepartmentId = 3,
                FirstName = "Avril",
                LastName = "Lavigne",
                Gender = EnumGender.Female
            });
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="employee"></param>
        /// <returns></returns>
        public Task Add(Employee employee)
        {
            employee.Id = _employees.Max(x => x.Id) + 1;
            employee.Fired = false;
            _employees.Add(employee);
            return Task.CompletedTask;
        }

        /// <summary>
        /// 更新指定项
        /// </summary>
        /// <param name="employee"></param>
        /// <returns></returns>
        public Task Update(Employee employee)
        {
            return Task.Run(function: () =>
             {
                 var model = _employees.FirstOrDefault(x => x.Id == employee.Id);
                 if (model != null)
                 {
                     model = employee;
                     return model;
                 }
                 return null;
             });
        }

        /// <summary>
        /// 删除指定项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Task<bool> Delete(int id)
        {
            return Task.Run(function: () =>
             {
                 var model = _employees.FirstOrDefault(x => x.Id == id);
                 if (model != null)
                 {
                     return _employees.Remove(model);
                 }
                 return false;
             });
        }


        /// <summary>
        /// 获取所有员工信息
        /// </summary>
        /// <returns></returns>
        public Task<IEnumerable<Employee>> GetAll()
        {
            return Task.Run(function: () => _employees.AsEnumerable());
        }

        /// <summary>
        /// 根据部门id获取部门下所有员工信息
        /// </summary>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        public Task<IEnumerable<Employee>> GetEmployeeByDepartmentId(int departmentId)
        {
            return Task.Run(function: () => _employees.Where(x => x.DepartmentId == departmentId));
        }

        /// <summary>
        /// 获取指定员工详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Task<Employee> GetEmployeeById(int id)
        {
            return Task.Run(function: () => _employees.FirstOrDefault(x => x.Id == id));
        }

        /// <summary>
        /// 解雇指定员工
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Task<Employee> Fire(int id)
        {
            return Task.Run(function: () =>
             {
                 var employee = _employees.FirstOrDefault(e => e.Id == id);
                 if (employee != null)
                 {
                     employee.Fired = employee.Fired ? false : true;
                     return employee;
                 }
                 return null;
             });

        }
    }
}
